package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.hnu.qq.global.CheckCode;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class ShowYZMDialog extends JDialog{

	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};
	private JTextField textField;
	private JLabel yzm;
	private CheckCode code;
	private JLabel lblNewLabel;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ShowYZMDialog dialog = new ShowYZMDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ShowYZMDialog() {
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setSize(340, 200);
	}
	
	
	
	public ShowYZMDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setSize(340, 200);
	}

	public ShowYZMDialog(Frame owner, boolean modal) {
		super(owner, modal);
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setSize(340, 200);
	}

	private void init() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		setBounds(100, 100, 340, 198);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JLabel label = new JLabel("登陆出错超过三次，请输入验证码：");
			contentPanel.add(label, BorderLayout.NORTH);
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(new FormLayout(new ColumnSpec[] {
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,}));
			{
				code = new CheckCode();
			}
			yzm = new JLabel("");
			code = CheckCode.createCheckCode();
			yzm.setIcon(new ImageIcon(code.getBuffImg()));
			yzm.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent e) {
					if(e.getSource() == (yzm)){
						yzm.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
					}
				}
				public void mouseClicked(MouseEvent e) {
					if(e.getSource() == (yzm)){
						code = CheckCode.createCheckCode();
						yzm.setIcon(new ImageIcon(code.getBuffImg()));
						yzm.validate();
					}
					
				}
			});
			panel.add(yzm, "4, 2");
			{
				JLabel label = new JLabel("验证码：");
				panel.add(label, "2, 4");
			}
			{
				textField = new JTextField();
				panel.add(textField, "4, 4, left, default");
				textField.setColumns(10);
			}
			{
				lblNewLabel = new JLabel("");
				panel.add(lblNewLabel, "6, 4");
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(!textField.getText().equalsIgnoreCase(code.getCheckCodeStr())){
							lblNewLabel.setText("验证码错误");
							code = CheckCode.createCheckCode();
							yzm.setIcon(new ImageIcon(code.getBuffImg()));
							lblNewLabel.validate();
							yzm.validate();
						}else{
							ShowYZMDialog.this.dispose();
						}
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	@Override
	protected void processWindowEvent(WindowEvent e) {
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
			if(!textField.getText().equalsIgnoreCase(code.getCheckCodeStr())){
				JOptionPane.showMessageDialog(ShowYZMDialog.this, "验证码不正确");
				return;
			}
		}
		super.processWindowEvent(e);
	}
	
	
	
}
